﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using System.Net;
using System.IO;
using System.Text;
using MM.Common;
using MM.DBUtility;

namespace MobileManager
{
    public partial class UploadMobileInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    Encoding encode = System.Text.Encoding.GetEncoding("gbk");
                    Stream ReceiveStream = Request.InputStream;
                    StreamReader readStream = new StreamReader(ReceiveStream, encode);
                    Char[] read = new Char[256];
                    int count = readStream.Read(read, 0, 256);
                    string strXML = null;
                    while (count > 0)
                    {
                        strXML += new String(read, 0, count);
                        count = readStream.Read(read, 0, 256);
                    }
                    readStream.Close();

                    if (!string.IsNullOrEmpty(strXML))
                    {
                        if (GetMobileInfo(strXML))
                            Response.Write("ok");
                        else
                            Response.Write("err");
                    }
                    else Response.Write("err");

                }
            }
            catch (Exception ex)
            {
                LogHelp.WriteLogInfo(ex.ToString());
                Response.Write("err");
            }
        }
        private bool GetMobileInfo(string strXML)
        {
            bool bolResult = false;
            if (strXML.Contains("|"))
            {
                string strMobile = string.Empty, strIMSI = string.Empty, strCNO = string.Empty, strSQL = string.Empty;
                StringBuilder sbSQL = new StringBuilder();
                string[] arg1 = strXML.Split('|');
                foreach (string strTemp in arg1)
                {
                    if (strTemp.Contains("A"))
                    {
                        string[] arg2 = strTemp.Split('A');
                        if (arg2.Length == 3)
                        {
                            strMobile = arg2[0];
                            strIMSI = arg2[1];
                            strCNO = arg2[2];

                            sbSQL.Append(string.Format(" SELECT  '{0}' Mobile,'{1}' IMSI,'{2}' CNO UNION ", strMobile, strIMSI, strCNO));
                        }
                    }
                }

                if (sbSQL.Length > 0)
                {
                    strSQL = sbSQL.ToString();
                    strSQL = "INSERT INTO T_MobileAndSIM(Mobile,IMSI,CNO) SELECT * FROM (" + strSQL.Remove(strSQL.LastIndexOf("UNION")) + ") AS A WHERE A.Mobile NOT IN (SELECT Mobile FROM T_MobileAndSIM)";
                    DbHelperSql.ExecuteSql(strSQL);
                    bolResult = true;
                }
            }
            return bolResult;
        }
    }
}
